#include <bits/stdc++.h>

using namespace std;

struct comp1 {
    bool operator() (vector<int>& a, vector<int>& b) {
        if (a[1] < b[1]) {
            return true;
        } else if (a[1] == b[1]) {
            if (a[2] < b[2]) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
};

struct comp2 {
    bool operator() (vector<int>& a, vector<int>& b) {
        if (a[2] < b[2]) {
            return true;
        } else if (a[2] == b[2]) {
            if (a[1] < b[1]) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
};

int main() {
    int machine_nums;
    cin >> machine_nums;
    // cout << machine_nums;
    cin.ignore();

    // resoures
    vector<vector<int>> machine_resources(machine_nums);
    int tmp;
    for (int i = 0; i < machine_nums; ++i) {
        for (int j = 0; j < 5; ++j) {
        cin >> tmp;
        // cout << "ok";
        // cout << tmp << " ";
        machine_resources[i].push_back(tmp);
        cin.ignore();
        }
        
    }

    // request
    int max_req_num, stragty;
    vector<int> req_resource(5);
    cin >> max_req_num;
    cin >> stragty;
    // cout << max_req_num;
    for (int i = 1; i < 5; ++i) {
        cin >> tmp;
        req_resource[i] = tmp;
    }

    vector<vector<int>> satify_resources;
    // for (auto  i = machine_resources.begin() ; i != machine_resources.end(); ++i) {
    //     if (req_resource[3] == 9 && (req_resource[4] == 2)) {
    //         if ((*i)[1] < req_resource[1] || ((*i)[2] < req_resource[2])) {}
    //     }
    // }
    for (int i = 0; i < machine_nums; ++i) {
        if (req_resource[3] == 9 && (req_resource[4] == 2)) {
            if (machine_resources[i][1] < req_resource[1] || (machine_resources[i][2] < req_resource[2])) {
                // satify_resources.push_back(machine_resources[i])
                continue;
            } else {
                satify_resources.push_back(machine_resources[i]);
            }
        } else if (req_resource[3] == 9) {
            if (machine_resources[i][1] < req_resource[1] || (machine_resources[i][2] < req_resource[2]) || (machine_resources[i][4] != req_resource[4])) {
                continue;
            } else {
                satify_resources.push_back(machine_resources[i]);
            }
        } else if (req_resource[4] == 2) {
            if (machine_resources[i][1] < req_resource[1] || (machine_resources[i][2] < req_resource[2]) || (machine_resources[i][3] != req_resource[3])) {
                continue;
            } else {
                satify_resources.push_back(machine_resources[i]);
            }
        } else if (machine_resources[i][1] < req_resource[1] || (machine_resources[i][2] < req_resource[2]) || (machine_resources[i][3] != req_resource[3]) || (machine_resources[i][4] != req_resource[4])) {
            continue;
        } else {
            satify_resources.push_back(machine_resources[i]);
        }
    }

    string output;
    int satify_num = satify_resources.size();
    if (satify_num == 0) {
        cout << 0;
    } else if (satify_num <= max_req_num){
        output += to_string(satify_num);
        output += " ";
        for (int  i = 0; i <satify_num; ++i) {
            output += to_string(satify_resources[i][0]);
            output += " ";
        }
        output.pop_back();
        cout << output;
    } else {
        // 使用优先队列进行比较结果对不对！！
        if (stragty == 1) {
            sort(satify_resources.begin(), satify_resources.end(), comp1());
        } else {
            sort(satify_resources.begin(), satify_resources.end(), comp2());
        }
        output += to_string(max_req_num);
        output += " ";
        for (int  i = 0; i < max_req_num; ++i) {
            output += to_string(satify_resources[i][0]);
            output += " ";
        }
        output.pop_back();
        cout << output;
    }
    return 0;
}